package com.chao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chao.entity.Class;
import com.chao.entity.Major;
import com.chao.entity.Msg;
import com.chao.service.impl.ClassServiceImpl;
import com.chao.utils.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class ClassController {


    @Autowired
    ClassServiceImpl classService;


    // 条件查询(根据所属专业)
    @GetMapping("/class_query")
    @ResponseBody
    public Msg class_query(Class clazz) {
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        wrapper.eq("c_major", clazz.getCMajor());
        List<Class> classes = classService.list(wrapper);
        return Msg.success().add("classes", classes);
    }

    // 更新数据
    @PutMapping("/class/{cId}")
    @ResponseBody
    public Msg class_update(@PathVariable Integer cId,
                            @Valid Class clazz) {
        clazz.setCId(cId);
        clazz.setCDate(new Date());
        classService.updateById(clazz);
        return Msg.success();
    }


    // 批量删除 和 单个删除一起
    @DeleteMapping("/class/{cIds}")
    @ResponseBody
    public Msg class_delete(@PathVariable String cIds) {
        if (cIds.contains("-")) {
            String[] strings = cIds.split("-");
            List<Integer> integerList = TypeConversion.strArrToIntegerList(strings);
            classService.removeBatchByIds(integerList);
        } else {
            classService.removeById(Integer.parseInt(cIds));
        }
        return Msg.success();
    }


    // 实现添加功能
    @PostMapping("/class")
    @ResponseBody
    public Msg class_add(@Valid Class clazz) {
        clazz.setCDate(new Date());
        classService.addOfPrimary(clazz);
        return Msg.success();
    }

    @GetMapping("/class")
    @ResponseBody
    public Msg major(@RequestParam(defaultValue = "1") Integer pn) {
        Page<Class> page = new Page<>(pn, 5);
        Page<Class> classPage = classService.selectAll(page);
        List<Class> classes = classPage.getRecords();
        return Msg.success().add("page", page).add("classes", classes);
    }

    // 检查 专业 是否已存在
    @GetMapping("/checkClass")
    @ResponseBody
    public Msg checkClass(@RequestParam String cName) {
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        wrapper.eq("c_name", cName);
        boolean exists = classService.getBaseMapper().exists(wrapper);
        if (exists) {
            return Msg.fail().add("msg", "该班级已经存在");
        } else {
            return Msg.success();
        }

    }

    // 去 页面
    @GetMapping("/class_table")
    public String class_table() {
        return "class/class_table";
    }


}
